﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2013/3/26 星期二 19:33:29
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Sector7.Framework.EntityType;
using Sector7.Framework.DB;
using System.Collections;

namespace Sector7.Framework.DAO.Standard
{
    /// <summary>
    /// 数据元模板项数据访问对象
    /// </summary>
    public class DataTemplateItemDAO : DAOBase<S7_DataTemplateItem>, IDataTemplateItemDAO
    {
        #region Public functions
        public int Create(List<S7_DataTemplateItem> items)
        {
            string sql = @"
                INSERT INTO S7_DataTemplateItem
                        (MetadataId
                        ,TemplateId)
                    VALUES
                        (@MetadataId
                        ,@TemplateId)";

            using (DBHelper helper = new DBHelper())
            {
                return helper.ExecuteBatchNonQuery(sql,
                    items,
                    "TemplateItemId",
                    "MetadataId",
                    "TemplateId");
            }
        }

        public List<S7_DataTemplateItem> ReadAll(int templateId)
        {
            string sql = @"
                SELECT *
                    FROM S7_DataTemplateItem i
                        LEFT JOIN S7_Metadata md
                            ON md.MetadataId = i.MetadataId
                        LEFT JOIN S7_DataElement de
                            ON md.ElementId = de.ElementId
                        LEFT JOIN S7_ValueType vt
                            ON de.TypeId = vt.TypeId
                        LEFT JOIN S7_ValueDomain vd
                            on de.DomainId = vd.DomainId
                    WHERE i.TemplateId = @TemplateId";

            List<S7_DataTemplateItem> items = new List<S7_DataTemplateItem>();

            using (DBHelper helper = new DBHelper())
            {
                SqlDataReader reader = helper.ExecuteQuery(sql,
                    DBUtil.CreateSqlParameter(templateId, "TemplateId"));

                while (reader.Read())
                {
                    var valueType = new S7_ValueType();
                    DBUtil.ReadData(reader, valueType);

                    var valueDomain = new S7_ValueDomain();
                    DBUtil.ReadData(reader, valueDomain);

                    var metadata = new S7_Metadata();
                    DBUtil.ReadData(reader, metadata);

                    var item = new S7_DataTemplateItem();
                    DBUtil.ReadData(reader, item);

                    metadata.ValueType = valueType;
                    metadata.ValueDomain = valueDomain;
                    
                    item.Metadata = metadata;

                    items.Add(item);
                }
            }

            return items;
        }
        #endregion
    }
}
